<!-- 购物车页面 -->
<template>
  <view class="cart-container">
    <!-- 收货地址组件 -->
    <my-address></my-address>
    <view class="cart-title">
      <uni-icons type="shop" size="20" class="shop-icon"></uni-icons>
      <text>购物车</text>
    </view>
    <!-- 左滑删除组件  uni-swipe-action-->
    <uni-swipe-action>
      <block class="goods-item" v-for="(item,index) in cart" :key="index">
        <uni-swipe-action-item :right-options="options" @click="swipeItemClickHandler(item)">
          <!-- :showBox="true" 控制NumberBox；:showRadio="true" 控制勾选框是否显示 ；@radio-change="radioChangeHandler"控制勾选框是否勾选  -->
          <my-goods :showBox="true" :item="item" :showRadio="true" @radio-change="radioChangeHandler"
            @num-change="numberChangeHandler"></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    <!-- 引入结算组件 -->
    <my-settle></my-settle>
  </view>
</template>
<script>
  import minxBadge from '@/mixins/tabbar-badge.js'
  import {
    mapMutations
  } from 'vuex'
  import {
    mapState
  } from 'vuex'
  export default {
    mixins: [minxBadge],
    //渲染购物车里商品列表

    //1.先获取购物车仓库里的cart属性
    computed: {
      ...mapState('m_cart', ['cart'])
    },
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#C00000'
          }
        }]
      };
    },
    methods: {
      ...mapMutations('m_cart', ['updateGoodsState', 'updataNumber', 'removeGoodsById']),
      radioChangeHandler(e) {
        //调用cart.js中的更新方法updateGoodsState（）
        this.updateGoodsState(e)
      },
      //获取数量修改的
      numberChangeHandler(e) {
        console.log(e);
        this.updataNumber(e)
      },
      //删除购物车中的列表项
      swipeItemClickHandler(goods) {
        console.log(goods);
        //调用cart.js仓库里的删除方法
        this.removeGoodsById(goods)
      }
    }
  }
</script>

<style lang="scss">
  .cart-container {
    padding-bottom: 50px;
  }

  .cart-title {
    display: flex;
    flex-wrap: wrap;
    height: 40px;
    //垂直居中
    align-items: center;
    padding-left: 5px;
    border-bottom: 1px solid #edefee;

    .shop-icon {
      margin-right: 10px;
      vertical-align: middle; // 确保图标和文字垂直对齐
    }

    text {
      font-size: 18px
    }
  }
</style>